package com.jinghao.ctws.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.jinghao.core.common.exception.JingHaoCustomerException;
import com.jinghao.core.common.service.impl.JingHaoBaseServiceImpl;
import com.jinghao.core.common.util.cnd.JhCnd;
import com.jinghao.ctws.entity.CwvPallet;
import com.jinghao.ctws.service.ICwvPalletService;
import com.jinghao.ctws.service.ICwvPublicSysService;
import com.jinghao.sys.entity.SysAccount;
import com.jinghao.sys.entity.SysAccountOrganization;
import com.jinghao.sys.entity.SysOrganization;
import com.jinghao.sys.service.ISysAccountOrganizationService;
import com.jinghao.sys.service.ISysAccountService;
import com.jinghao.sys.service.ISysOrganizationService;

@Service("cwvPublicSysService")
public class CwvPublicSysServiceImpl extends JingHaoBaseServiceImpl implements ICwvPublicSysService {
	//==================定制内容开始======================
//在这里修改代码
	@Resource
	public ICwvPalletService cwvPalletService;
	
	@Resource
	public ISysAccountOrganizationService sysAccountOrganizationService;
	
	@Resource
	public ISysAccountService sysAccountService;
	
	@Resource
	public ISysOrganizationService sysOrganizationService;

	@Override
	public void deleteOrg(SysOrganization model) {
		if (StringUtils.isEmpty(model.getJhorgUuid())){
			throw new JingHaoCustomerException("仓库UUID不能为空，请重新选择!");
		}
		//检测账户
		JhCnd cnd=new JhCnd();
		SysAccountOrganization account=new SysAccountOrganization();
		account.setJhamoJhorgUuid(model.getJhorgUuid());
		cnd.andEquals("jhamoJhorgUuid");
		account.setJhCnd(cnd);
		List<Map<String,Object>> list=this.sysAccountOrganizationService.queryMapList(account);
		if(list!=null&&list.size()>0){
			throw new JingHaoCustomerException("该仓库还存在相关账户，不允许进行删除操作!");
		}
		
		cnd.reset();
		cnd.andEquals("fjhorgUuid");
		CwvPallet cwvPallet=new CwvPallet();
		cwvPallet.setFjhorgUuid(model.getJhorgUuid());
		cwvPallet.setJhCnd(cnd);
		list=this.cwvPalletService.queryMapList(cwvPallet);
		if(list!=null&&list.size()>0){
			throw new JingHaoCustomerException("该仓库已存在相关托盘数据，不允许进行删除操作!");
		}
		
		//删除仓库
		this.sysOrganizationService.delete(model);
	}
	
	@Override
	public void updateCheckIsLoginByToken(SysAccount model) {
		 Map<String,Object> map=model.getObjMap();
		 String token=map.get("accessToken")!=null?map.get("accessToken").toString():"";
		if(null==token||token.trim().equals("")){
			throw new JingHaoCustomerException("accessToken值不能为空！");
		}
		JhCnd cnd=new JhCnd();
		cnd.andEquals("jhactAccessToken");
		model.lnkCnd(cnd);
		model.setJhactAccessToken(token);
		List<SysAccount> list =this.sysAccountService.queryModelList(model);
		Map<String,Object> dataMap=new HashMap<String,Object>();
		if(list==null||list.size()<=0){
			 throw new JingHaoCustomerException("-1","账户未进行登录或已超时，请重新登录！");
		}else{
			 String jhactUuid=list.get(0).getJhactUuid();
			 cnd.reset();
			 cnd.andEquals("jhamoJhactUuid");
			 SysAccountOrganization sysAccountOrganization=new SysAccountOrganization();
			 sysAccountOrganization.setJhamoJhactUuid(jhactUuid);
			 sysAccountOrganization.setJhCnd(cnd);
			 List<Map<String,Object>> resultlist=this.sysAccountOrganizationService.queryMapList(sysAccountOrganization);
			 if(resultlist==null||resultlist.size()==0){
				 throw new JingHaoCustomerException("-1","未指定账户所属仓库！");
			 }
			 model.setMapList(resultlist);
		}
	}

	
	//==================定制内容结束======================
}
